package com.pl.service.impl;

import com.alibaba.excel.EasyExcel;
import com.pl.entity.Device;
import com.pl.entity.Devicefl;
import com.pl.dao.DeviceflDao;
import com.pl.lister.deviceReadLister;
import com.pl.lister.deviceflReadLister;
import com.pl.service.DeviceflService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * (Devicefl)表服务实现类
 *
 * @author makejava
 * @since 2024-05-05 21:53:25
 */
@Service("deviceflService")
public class DeviceflServiceImpl implements DeviceflService {
    @Autowired
    private DeviceflDao deviceflDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Devicefl queryById(Integer id) {
        return this.deviceflDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @Override
    public List<Devicefl> queryByPage(Map<String, Object> map) {
        return this.deviceflDao.queryByPage(map);
    }

    /**
     * 新增数据
     *
     * @param devicefl 实例对象
     * @return 实例对象
     */
    @Override
    public boolean insert(Devicefl devicefl) {
        int num = this.deviceflDao.insert(devicefl);
        if (num > 0) {
            return true;
        }
        return false;
    }

    /**
     * 修改数据
     *
     * @param devicefl 实例对象
     * @return 实例对象
     */
    @Override
    public boolean update(Devicefl devicefl) {
        int num = this.deviceflDao.update(devicefl);
        if (num > 0) {
            return true;
        }
        return false;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(int id) {
        return this.deviceflDao.deleteById(id) > 0;
    }
//批量删除
    @Override
    public boolean deleteflBatch(List<Integer> list) {
        int i = deviceflDao.deleteBatch(list);
        if (i > 0) {
            return true;
        }
        return false;
    }


    /**
     * 批量添加，用于文件导入时 配合监听器使用
     *
     * @param deviceflsList
     * @return
     */
    @Override
    public boolean inserts(List<Devicefl> deviceflsList) {

        int num = deviceflDao.insertBatch(deviceflsList);
        if (num > 0) {
            return true;
        }
        return false;
    }


    //导入
    @Override
    public String importDeviceflFile(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), Devicefl.class, new deviceflReadLister(this)).sheet("设备分类信息").doReadSync();
            return "import_success";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "fail";
    }
//导出
    @Override
    public void exportDeviceflFile(HttpServletResponse response) {
        List<Devicefl> devicefl = deviceflDao.finAll();
        try {
            EasyExcel.write(response.getOutputStream(), Devicefl.class).sheet("设备分类信息").doWrite(devicefl);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
//查询全部
    @Override
    public boolean finAll() {
        List<Devicefl> devicefls = deviceflDao.finAll();
        if (devicefls != null) {
            return true;
        }
        return false;
    }
}
